/*
 * This file is part of andExplorer, android.ovhoo.com
 * Copyright (C) 2007 Mohamed ZALIM <mzalim@ovhoo.com>
 *
 * andExplorer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * andExplorer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl.html>.
 */
package com.ovhoo.android.media;

import java.io.IOException;

import com.ovhoo.android.file.FileInterface;

import android.content.Context;
import android.media.MediaPlayer;

public class MultimediaPlayer {
	private static MultimediaPlayer uniqueInstance; 
	//private Context context;
	private MediaPlayer mediaPlayer; //on at a time
	
	
	private MultimediaPlayer(){
		this.mediaPlayer = null;
	}
	
	public static MultimediaPlayer getInstance(){
		if(MultimediaPlayer.uniqueInstance == null){
			MultimediaPlayer.uniqueInstance = new MultimediaPlayer();
		}
		
		return MultimediaPlayer.uniqueInstance;
	}
	
	/**
	 * Start Playing the given file
	 * @param context 
	 * @param file the audio file to play
	 */
	public synchronized boolean prepare(Context context, FileInterface file){
		if (this.mediaPlayer != null){
			this.mediaPlayer.release();
		}
		
		this.mediaPlayer = MediaPlayer.create(context, file.getUri());
		if (this.mediaPlayer != null){
			try {
				this.mediaPlayer.prepare();
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return true;
		}
		else{
			return false;
		}
	}

	public boolean isPlaying() {
		if (this.mediaPlayer != null){
			try {
				return this.mediaPlayer.isPlaying();
			} catch (IllegalStateException e){
				e.printStackTrace();
				return false;
			}
		}
		else{
			return false;
		}
	}

	public synchronized  void pause() {
		if (this.mediaPlayer != null){
			try {
				this.mediaPlayer.pause();
			} catch (IllegalStateException e){
				e.printStackTrace();
			}
			
		}
		
	}

	public  synchronized int getDuration() {
		if (this.mediaPlayer != null){
			try {
				return this.mediaPlayer.getDuration();
			} catch (IllegalStateException e){
				e.printStackTrace();
				return 0;
			}
			
		}
		else{
			return 0;
		}
	}

	public  synchronized int getCurrentPosition() {
		if (this.mediaPlayer != null){
			try {
				return this.mediaPlayer.getCurrentPosition();
			} catch (IllegalStateException e){
				e.printStackTrace();
				return 0;
			}
		}
		else{
			return 0;
		}
	}

	public synchronized  void start() {
		if (this.mediaPlayer != null){
			
			try {
				this.mediaPlayer.start();
			} catch (IllegalStateException e){
				e.printStackTrace();
			}
			
		}
	}

	public  synchronized void seekTo(int progress) {
		if (this.mediaPlayer != null){
			try {
				this.mediaPlayer.seekTo(progress);
			} catch (IllegalStateException e){
				e.printStackTrace();
			}
		}
		
	}
}
